package at.codejedi.sto.gui.combat;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import at.codejedi.sto.gui.combat.tree.CombatLogTree;
import at.codejedi.sto.gui.lazy.LazyPanel;
import at.codejedi.sto.model.combat.CombatLog;

public class CombatLogView extends LazyPanel implements ICombatLogView {

	private static final long serialVersionUID = -8331198522448550609L;

	private List<ActionListener> closeListeners = new ArrayList<ActionListener>();

	private List<ActionListener> copyToClipboardListener = new ArrayList<ActionListener>();

	JPanel detailPanel;

	private List<ActionListener> newBuildListeners = new ArrayList<ActionListener>();

	CombatLogTree treeView;

	public CombatLogView() {
		initUI();
	}

	public void addCloseActionListener(ActionListener l) {
		closeListeners.add(l);
	}

	public void addCopyToClipboardListener(ActionListener l) {
		copyToClipboardListener.add(l);
	}

	public void addCreateNewBuildActionListener(ActionListener l) {
		newBuildListeners.add(l);
	}

	@Override
	public void addDetailSelectedActionListener(ActionListener l) {
		treeView.addDetailSelectedActionListener(l);

	}

	@Override
	public void copyDetailsToClipboard() {
		for (ActionListener l : copyToClipboardListener) {
			l.actionPerformed(new ActionEvent(this, 0, null));
		}
	}

	private CombatLogTree createCombatLogTree() {
		return new CombatLogTree();
	}

	private JScrollPane createDetailScrollPane() {
		detailPanel = new JPanel();
		detailPanel.addContainerListener(new ContainerListener() {

			@Override
			public void componentAdded(ContainerEvent e) {
				Component c = e.getComponent();
				c.setVisible(false);
				c.setVisible(true);

			}

			@Override
			public void componentRemoved(ContainerEvent e) {
				// TODO Auto-generated method stub

			}
		});

		JScrollPane detailView = new JScrollPane(detailPanel);
		detailPanel.setLayout(new GridLayout(1, 0, 0, 0));
		return detailView;
	}

	@Override
	public void createNewBuild() {
		for (ActionListener l : newBuildListeners) {
			l.actionPerformed(new ActionEvent(this, 0, null));
		}
	}

	@Override
	public void displayCombatLog(CombatLog log) {
		treeView.displayCombatLog(log);

	}

	@Override
	public void displayDetail(IDetailView detailViewToDisplay) {
		detailPanel.removeAll();
		JPanel panel = detailViewToDisplay.getPanel();
		detailPanel.add(panel);
		panel.setVisible(false);
		panel.setVisible(true);

	}

	private void initUI() {

		// Set up the content pane.
		setLayout(new GridLayout(1, 1));

		treeView = createCombatLogTree();

		JScrollPane detailView = createDetailScrollPane();

		// Create a split pane with the two scroll panes in it.
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				treeView, detailView);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation(150);

		// Provide minimum sizes for the two components in the split pane
		Dimension minimumSize = new Dimension(100, 50);
		treeView.setMinimumSize(minimumSize);
		detailView.setMinimumSize(minimumSize);

		add(splitPane);

	}

	@Override
	public void requestClose() {
		for (ActionListener l : closeListeners) {
			l.actionPerformed(new ActionEvent(this, 0, null));
		}
	}
}
